using System.Collections.Generic;
using Yoozoo.Managers.NetworkV2;

namespace Yoozoo.Framework.Network.Helpers
{
    public class GTAReceivedInfoHelper
    {
        // 消息简化为仅记录一个索引号
        public static int RecvMsgIdx = 0;

        private static List<int> recvMsgBuffer = new List<int>();
 
        /// <summary>
        /// 获取最后收到的消息索引号
        /// </summary>
        /// <returns> 消息索引号 </returns>
        public static int GetReceivedIndex()
        {
            return RecvMsgIdx;
        }

        /// <summary>
        /// 获取序列化包的索引号
        /// </summary>
        /// <returns></returns>
        public static int GetMsgIndex()
        {
            return GTAPacketSerializerHelper.msgIndex;
        }

        /// <summary>
        /// 更新最后收到的消息索引号
        /// </summary>
        /// <param name="msgIdx"></param>
        public static void UpdateReceivedIndex(int msgIdx)
        {
            RecvMsgIdx = msgIdx;
        }

        /// <summary>
        /// 把消息的sn存入缓存，给lua调用的时候再取
        /// 不能直接取是因为收到协议后C#会将他存入队列，然后每帧异步去给lua调用，因此缺少一个存SN的地方，IBasicPacket又不能热更，所以存在这里
        /// </summary>
        /// <param name="sn"></param>
        /// <param name="messageId"></param>
        public static void AddReceivedMsgIndexToBuffer(int sn,int messageId)
        {
            var result = sn * 1000000 + messageId;
            recvMsgBuffer.Add(result);
        }
        
        /// <summary>
        /// 根据协议id从缓存里获取sn
        /// </summary>
        /// <param name="messageId"></param>
        /// <returns></returns>
        public static int GetReceivedMsgIndexFromBuffer(int messageId)
        {
            for (int i = 0; i < recvMsgBuffer.Count; i++)
            {
                var data = recvMsgBuffer[i];
                var msgId = data % 1000000;
                if (msgId == messageId)
                {
                    recvMsgBuffer.RemoveAt(i);
                    return (data - msgId) / 1000000;
                }
            }
            return -1;
        }
        
        /// <summary>
        /// 打印收到的消息索引号
        /// </summary>
        /// <param name="msgIdx"></param>
        public static void PrintReceivedIndex(int msgIdx,int msgId=0)
        {
            UnityEngine.Debug.LogWarningFormat("屏蔽消息信息 msgIdx:{0}  currentRecvMsgIdx:{1}   msgId = {2} ",msgIdx,RecvMsgIdx,msgId);
        }

        /// <summary>
        /// 重置索引
        /// </summary>
        public static void ResetIndex()
        {
            RecvMsgIdx = 0;
            ResetPacketSerializerMsgIndex();
        }

        /// <summary>
        /// 重置序列化包的索引号
        /// </summary>
        public static void ResetPacketSerializerMsgIndex()
        {
            GTAPacketSerializerHelper.msgIndex = 0;
        }
        
        /// <summary>
        /// 判定消息是否可用
        /// </summary>
        /// <param name="msgIdx"></param>
        /// <returns></returns>
        public static bool CheckMsgAvaliable(int msgIdx)
        {
            if (msgIdx == 0)
            {
                return true;
            }

            //如果正在重连过程中，收到序号1的协议，说明服务器走了大重连，需要重置
            if (msgIdx == 1 && isReconnecting)
            {
                return true;
            }
            return msgIdx > RecvMsgIdx;
        }

        private static bool isReconnecting = false;

        public static void SetIsReconnecting(bool isReconnecting)
        {
            GTAReceivedInfoHelper.isReconnecting = isReconnecting;
        }

        public static bool IsReconnecting => isReconnecting;
    }
}